import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/Login'
// import Layout from '@/views/Layout'
import Home from '@/views/Home'
// import User from '@/views/User'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEdit from '@/views/User/UserEdit'
// import Chat from '@/views/Chat'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    // component: Login
    component: () => import('@/views/Login'),
    beforeEnter: (to, from, next) => {
      if (store.state.token.length) {
        next(false)
      } else {
        // 其他情况全部放行
        next()
      }
    }
  },
  {
    path: '/layout',
    // component: Layout,
    component: () => import('@/views/Layout'),
    redirect: '/layout/home',
    // 二级路由
    children: [{
      path: 'home',
      component: Home,
      // component: () => import('@/views/Home')
      meta: {
        // 这个页面需要设置滚动条
        isScoll: true,
        // 离开此路由时,滚动条的位置
        top: 0
      }
    },
    {
      path: 'user',
      // component: User
      component: () => import('@/views/User')
    }
    ]
  },
  {
    path: '/search',
    // component: Search
    component: () => import('@/views/Search')
  },
  {
    // 动态路由:路径上传值
    // 提前爱规则里使用/:变量名形式定义好
    // 要跳到当前路由路径上,必须在第二段
    path: '/search/:keywords',
    // component: SearchResult
    component: () => import('@/views/Search/SearchResult')
  },
  {
    path: '/article',
    // component: ArticleDetail
    component: () => import('@/views/ArticleDetail')
  },
  {
    path: '/user/edit',
    // component: UserEdit
    component: () => import('@/views/User/UserEdit')
  },
  {
    path: '/chat',
    // component: Chat
    component: () => import('@/views/Chat')
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫
// router.beforeEach((to, form, next) => {
//   // 需求:登录状态下, 想要去/login路上,留在原地
//   if (store.state.token.length > 0 && to.path === '/login') {
//     next(false)// 原地留下
//   } else {
//     // 其他情况全部方行
//     next()
//   }
// })

// 如果有token,但是需要验证身份的功能(后盾接口:点赞/评论/反馈)->token带给后台
// axios响应拦截器(判断token过期, 清除token, 然后跳转路由, 然后路由全局前置守卫进入判断, 没有token放行到登录页面)

export default router
